<?php


namespace app\api\controller\v20;


use app\api\controller\v2\Auth;
use app\common\cachemodel\live\LiveRecordingInfo;
use app\common\cachemodel\MallUserAddress;
use app\common\lib\SensorsData;
use app\common\lib\Util;

/**
 * 地址管理 v2.3.0
 * Class AddressManage
 * @package app\api\controller\v20
 */
class AddressManage extends Auth
{
    /**
     * 地址列表
     * @throws \Exception
     */
    public function index()
    {
        $uid = (int)$this->user['id'];

        $addressData = (new MallUserAddress())->getAddressList($uid);

        foreach ($addressData as $key => $addressDatum)
        {
            $addressData[$key]['id'] = (int)$addressDatum['id'];
            $addressData[$key]['is_default'] = (int)$addressDatum['is_default'];
            $addressData[$key]['is_faraway'] = (int)$addressDatum['is_faraway'];
        }

        return show(config('code.success'), '请求成功', $addressData);
    }

    /**
     * 添加或者编辑地址
     * @throws \Exception
     */
    public function addOrEdit()
    {
        $id = input('id/d', 0);
        $liveId = input('live_id/d', 0);
        $liveRecordId = input('live_record_id/d',0);
        (new LiveRecordingInfo())->checkRecordId($liveId, $liveRecordId);
        $uid = (int)$this->user['id'];

        $addressData = [
            'name' => input('name/s', ''),
            'phone' => input('phone/s', ''),
            'country' => input('country/s', ''),
            'province' => input('province/s', ''),
            'city' => input('city/s', ''),
            'address_desc' => input('address_desc/s', ''),
            'is_default' => input('is_default/d', 0),
        ];

        if (Util::commentCheck($uid, $addressData))
        {
            return show(config('code.error'), '请求频繁，请稍后再试～');
        }

        $result = (new MallUserAddress())->addOrEdir($uid, $id, $addressData);

        if ($result['res'])
        {
            $distinctId = $this->request->header('distinct-id', '');
            if ($distinctId)
            {
                #神策打点 67 收货地址 ReceiverHandle
                $sensorsTrackData = [
                    'receiver_name' => $addressData['name'],// 收货人姓名
                    'receiver_tel' => $addressData['phone'],// 收货人手机号
                    'receiver_province' => $addressData['country'],// 收货省份
                    'receiver_city' => $addressData['province'],// 收货市区
                    'receiver_address' => $addressData['city'] . $addressData['address_desc'],// 具体地址
                    'is_default' => $addressData['is_default'] === 1,// 是否设置为默认地址
                    'handle_name' => $id ? '修改' : '新建',// 操作类型
                    'live_id' => $liveId,
                    'live_record_id' => $liveRecordId,
                ];
                SensorsData::track($uid, true, 'ReceiverHandle', $sensorsTrackData);
            }
            return show(config('code.success'), '请求成功', $result['data']);
        } else
        {
            return show(config('code.error'), $result['msg']);
        }
    }

    /**
     * 删除地址
     */
    public function del()
    {
        $id = input('id/d', 0);
        $uid = (int)$this->user['id'];

        $result = (new MallUserAddress())->addressDel($uid, $id);

        if ($result['res'])
        {
            return show(config('code.success'), '删除成功');
        } else
        {
            return show(config('code.error'), $result['msg']);
        }
    }
}
